package com.bekdyf.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.extra.tokenizer.Result;
import cn.hutool.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping(value = "/captcha")
public class HuToolController {
    /**
     * 获取图形验证码
     * http://localhost:9999/JXWorkApi/captcha/getShearCaptcha
     *
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getShearCaptcha", method = RequestMethod.GET)
    public Result getShearCaptcha(HttpServletRequest request, HttpServletResponse response) {
        OutputStream out = null;
        try {
            try {
                out = response.getOutputStream();// 取得输出流
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 自定义验证码内容为四则运算方式
            RandomGenerator randomGenerator = new RandomGenerator("2g3f4e56c7b8a9hkmnpqrstwxzABCEFGHKMNRSTWXZ", 4);
            CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(150, 45, 6, 2);
            captcha.setGenerator(randomGenerator);
            captcha.setFont(new Font("宋体", Font.PLAIN, 38));
            captcha.createCode();
            captcha.write(out);
            //验证图形验证码的有效性，返回boolean值
            System.out.println(captcha.getCode());
            boolean checkPass = captcha.verify(captcha.getCode());
            // 将生成的验证码code放入sessoin中
            request.getSession().setAttribute("code", captcha.getCode());
            out.flush();  // 将缓存中的数据立即强制刷新, 将缓冲区的数据输出到客户端浏览器
            out.close(); // 关闭输出流
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 验证图形码（这一步其实可以放到登录拦截器中去做）
     * http://localhost:9999/JXWorkApi/captcha/verifyCode?code=e2xr
     *
     * @param inCode 前端输入的图形验证码code
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/verifyCode", method = RequestMethod.GET)
    public Result verifyCode(String inCode, HttpServletRequest req) {

        // 获取存放在session中的验证码
        String code = (String) req.getSession().getAttribute("code");
        // 获取页面提交的验证码
//        String inputCode = req.getParameter("code");
        String inputCode = inCode;
        if (code.toLowerCase().equals(inputCode.toLowerCase())) { // 验证码不区分大小写
            return null;
        }
        return null;
    }
}
